package com.zhongying.jf.util;

import com.hk.bank.exception.StatusException;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hyk on 2018/2/3.
 */
public class ResponseUtils {
    public static final int STATUS_SUCCSS = 200;
    public static final int STATUS_ERROR = 500;
    public static final String ERR_UNEXPECTED = "err.common.unexpected";
    public static final String PARAM_STATUS = "status";
    public static final String PARAM_ERROR_CODE = "errorCode";
    public static final String PARAM_ERROR_MESSAGE = "errorMessage";
    public static final String PARAM_ERROR_ARGS = "errorArgs";
    public static final String PARAM_DATA = "data";
    public static final String PARAM_OFFSET = "offset";
    public static final String PARAM_TOTAL_COUNT = "totalCount";
    public static final String PARAM_PAGE_SIZE = "pageSize";
    public static final String PARAM_EXTEND_PARAMS = "params";
    public static final String CT_JSON = "application/json; charset=utf-8";

    public ResponseUtils() {
    }

    protected static ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }

    public static void writeSuccessResult(HttpServletResponse response) throws Exception {
        writeSuccessResult(response, (Object)null);
    }

    public static void writeSuccessResult(HttpServletResponse response, Object data) throws Exception {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put("status", Integer.valueOf(200));
        if (data != null) {
            resultMap.put("data", data);
        }

        response.setContentType("application/json; charset=utf-8");
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    /** @deprecated */
    @Deprecated
    public static void writeSuccessPagingResult(HttpServletResponse response, int offset, int pageSize, Object data, int totalCount) throws Exception {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put("status", Integer.valueOf(200));
        resultMap.put("offset", offset);
        resultMap.put("pageSize", pageSize);
        resultMap.put("totalCount", totalCount);
        resultMap.put("data", data);
        response.setContentType("application/json; charset=utf-8");
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    /** @deprecated */
    @Deprecated
    public static void writeSuccessPagingResult(HttpServletResponse response, int offset, int pageSize, Object data, int totalCount, Map<String, Object> params) throws Exception {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put("status", Integer.valueOf(200));
        resultMap.put("offset", offset);
        resultMap.put("pageSize", pageSize);
        resultMap.put("totalCount", totalCount);
        resultMap.put("data", data);
        resultMap.put("params", params);
        response.setContentType("application/json; charset=utf-8");
        objectMapper.writeValue(response.getWriter(), resultMap);
    }



    public static void writeErrorResult(HttpServletResponse response, int status, String errorCode, String errorMessage, Object... errorArgs) throws Exception {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put("status", status);
        resultMap.put("errorCode", errorCode);
        resultMap.put("errorMessage", errorMessage);
        if (errorArgs != null) {
            resultMap.put("errorArgs", errorArgs);
        }

        response.setContentType("application/json; charset=utf-8");
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    public static void writeErrorResult(HttpServletResponse response, int status, StatusException e) throws Exception {
        writeErrorResult(response, status, e.getStatusCode(), e.getMessage(), e.getArguments());
    }

    public static void writeErrorResult(HttpServletResponse response, StatusException e) throws Exception {
        writeErrorResult(response, 500, e);
    }

    public static void writeErrorResult(HttpServletResponse response, Throwable t) throws Exception {
        writeErrorResult(response, 500, "err.common.unexpected", t.getMessage());
    }
}
